<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="460px" @close='close'>
    <el-form ref="form" :model="record" label-width="130px" :rules="rules" label-position="left" style="width:350px;margin: 0 30px;">
      <el-form-item style="width:325px;" :label="$t('类型')" prop="type">
        <el-select v-model="record.type" ref="type" :placeholder="$t('请选择')" size='small' style="width:217px;">
          <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <div v-if="record.type === 'tuple'">
        <el-form-item :label="$t('源地址')" prop="saddr">
          <el-input v-emoji v-model="record.saddr" ref="saddr" style="width:217px;" size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('目的地址')" prop="daddr">
          <el-input v-emoji v-model="record.daddr" ref="daddr" style="width:217px;" size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('源端口')" prop="sport">
          <el-input v-emoji v-model="record.sport" ref="sport" style="width:217px;" size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('目的端口')" prop="dport">
          <el-input v-emoji v-model="record.dport" ref="dport" style="width:217px;" size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('协议')" prop="prot">
          <el-input v-emoji v-model="record.prot" ref="prot" :placeholder="$t('协议输入范围:0-142')" style="width:217px;" size="small"></el-input>
        </el-form-item>
      </div>

      <div v-if="record.type === 'mac'">
        <el-form-item :label="$t('MAC地址')" prop="smac">
          <el-input v-emoji v-model="record.smac" ref="smac" style="width:217px;" size="small"></el-input>
        </el-form-item>
      </div>

      <div v-if="record.type === 'net'">
        <el-form-item :label="$t('起始地址')" prop="start">
          <el-input v-emoji v-model="record.start" ref="start" style="width:217px;" size="small"></el-input>
        </el-form-item>
        <el-form-item :label="$t('结束地址')" prop="end">
          <el-input v-emoji v-model="record.end" ref="end" style="width:217px;" size="small"></el-input>
        </el-form-item>
      </div>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">{{$t('确 定')}}</el-button>
      <el-button @click="close">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addStaticBlack } from '@/api/safe/blacklist'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'DynamicBlacklistDialog',
  data () {
    var validatePort = (rule, value, callback) => {
      if (value && (!this.isInteger(value) || Number(value) > 65535 || Number(value) < 0)) {
        callback(new Error(this.$t('请输入0-65535之间的整数')))
      } else {
        callback()
      }
    }
    var validateIp = (rule, value, callback) => {
      let ipv4 = /^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$/
      let ipv6 = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
      if (String(value).trim() === '') {
        // callback(new Error(this.$t('不能为空')))
        callback()
      } else if (ipv4.test(value) || ipv6.test(value)) {
        callback()
      } else {
        callback(new Error(this.$t('请输入正确的地址')))
      }
    }
    var validateProt = (rule, value, callback) => {
      if (value && (!this.isInteger(value) || Number(value) > 142 || Number(value) < 0)) {
        callback(new Error(this.$t('请输入0-142之间的整数')))
      } else {
        callback()
      }
    }
    var validateMac = (rule, value, callback) => {
      let reg = /(([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2})/
      if (String(value).trim() === '') {
        callback(new Error(this.$t('请输入MAC地址')))
      } else if (!reg.test(value)) {
        callback(new Error(this.$t('请输入正确的MAC地址')))
      } else {
        callback()
      }
    }
    var validateIpv4 = (rule, value, callback) => {
      let ipv4 = /^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$/
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!ipv4.test(value)) {
        callback(new Error(this.$t('请输入正确的Ipv4地址')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        type: 'tuple',
        saddr: '',
        daddr: '',
        sport: '',
        dport: '',
        prot: '',
        smac: '',
        start: '',
        end: ''
      },
      recordCopy: {
        type: 'tuple',
        saddr: '',
        daddr: '',
        sport: '',
        dport: '',
        prot: '',
        smac: '',
        start: '',
        end: ''
      },
      typeList: [
        { value: 'tuple', label: this.$t('五元组') },
        { value: 'mac', label: 'MAC' },
        { value: 'net', label: this.$t('IPV4范围') }
      ],
      rules: {
        saddr: [{ required: false, validator: validateIp, trigger: 'blur' }],
        daddr: [{ required: false, validator: validateIp, trigger: 'blur' }],
        sport: [{ required: false, validator: validatePort, trigger: 'blur' }],
        dport: [{ required: false, validator: validatePort, trigger: 'blur' }],
        prot: [{ required: false, validator: validateProt, trigger: 'blur' }],
        smac: [{ required: false, validator: validateMac, trigger: 'blur' }],
        start: [{ required: false, validator: validateIpv4, trigger: 'blur' }],
        end: [{ required: false, validator: validateIpv4, trigger: 'blur' }]
      }
    }
  },
  watch: {
    'record.type': {
      handler (newVal) {
        this.$refs.form.clearValidate()
        this.initClass(newVal)
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['type']
        selectClass(this.$refs, data)
        this.initClass(this.record.type)
      })
    },
    initClass (newVal) {
      this.$nextTick(() => {
        if (newVal === 'tuple') {
          const data = ['saddr', 'daddr', 'sport', 'dport', 'prot']
          inputClass(this.$refs, data)
        } else if (newVal === 'mac') {
          inputClass(this.$refs, ['smac'])
        } else if (newVal === 'net') {
          inputClass(this.$refs, ['start', 'end'])
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addStaticBlack(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('添加成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
